package  
{
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author hbb
	 */
	public class Interaction
	{
		private var _startX:Number;
		private var _startY:Number;
		
		private var _currX:Number;
		private var _currY:Number;
		
		public var onGesture:Function = _onGesture;
		
		private function _onGesture( params:Object ):void { }
		
		
		public function Interaction() 
		{
			GameManager.Singleton.canvas.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			GameManager.Singleton.canvas.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			GameManager.Singleton.canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		private function onMouseMove(e:MouseEvent):void 
		{
			var newX:Number = e.localX;
			var newY:Number = e.localY;
			_currX = newX;
			_currY = newY;
		}
		
		private function onMouseDown(e:MouseEvent):void 
		{
			_startX = _currX = e.localX;
			_startY = _currY = e.localY;
		}
		
		private function onMouseUp(e:MouseEvent):void 
		{
			var dx:Number = _currX - _startX;
			var dy:Number = _currY - _startY;
			
			if ( gesture_NoMove( dx, dy ) )
			{
				onGesture( { type:'nomove', x:_currX, y:_currY, startX:_startX, startY:_startY } );
			}
			else if ( gesture_HoriMove( dx, dy ) )
			{
				onGesture( { type:'horimove', x:_currX, y:_currY, startX:_startX, startY:_startY } );
			}
			else if ( gesture_VertMove( dx, dy ) )
			{
				onGesture( { type:'vertmove', x:_currX, y:_currY, startX:_startX, startY:_startY } );
			}
		}
		
		private function gesture_VertMove( dx:Number, dy:Number ):Boolean 
		{
			return !gesture_HoriMove( dx, dy );
		}
		
		private function gesture_HoriMove( dx:Number, dy:Number ):Boolean 
		{
			if ( 0 == dx ) return false;
			var k:Number = dy / dx;
			if ( ( k > 0 ) && ( k < 1 ) ) return true;
			if ( ( k < 0 ) && ( k >-1 ) ) return true;
			return false;
		}
		
		private function gesture_NoMove( dx:Number, dy:Number ):Boolean
		{
			return ( dx < 5 ) && ( dy < 5 );
		}
		
		
		
	}

}